package date;

import org.junit.Test;

import java.time.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Author:ZouHeng
 * Description:
 * Date:2021-07-06  15:44
 **/
public class TestNewDate {

    @Test
    public void testLocalDateTime() {
        //获取当前的日期时间
        LocalDateTime currentTime = LocalDateTime.now();
        System.out.println("当前时间：" + currentTime);

        //获取当前日期
        LocalDate localDate = currentTime.toLocalDate();
        System.out.println("当前日期：" + localDate);

        //获取年月日
        int year = currentTime.getYear();
        int month = currentTime.getMonthValue();
        Month month2 = currentTime.getMonth();
        int day = currentTime.getDayOfMonth();
        System.out.println("年：" + year);
        System.out.println("月：" + month);
        System.out.println("月：" + month2);
        System.out.println("日：" + day);

        //获取时分秒
        int hour = currentTime.getHour();
        int second = currentTime.getSecond();
        int minute = currentTime.getMinute();
        System.out.println("时：" + hour + "；分：" + minute + "：秒：" + second);

        //创建日期
        LocalDate createDate = LocalDate.of(2012, 5, 10);
        System.out.println("创建的日期：" + createDate);

        //创建时间
        LocalTime createTime = LocalTime.parse("20:12:32");
        LocalTime createTime2 = LocalTime.of(10, 23, 42);
        System.out.println("创建的时间：" + createTime);
        System.out.println("创建的时间：" + createTime2);

        //创建日期时间
        LocalDateTime createDateTime = LocalDateTime.of(createDate, createTime);
        System.out.println(createDateTime);
    }

    @Test
    public void testZoneDateTime() {
        // 获取当前时间日期
        ZonedDateTime dateTime = ZonedDateTime.now();
        System.out.println("当前时间：" + dateTime);
        ZonedDateTime date1 = ZonedDateTime.parse("2015-12-03T10:15:30+05:30[Asia/Shanghai]");
        System.out.println("date1: " + date1);

        ZoneId id = ZoneId.of("Europe/Paris");
        System.out.println("ZoneId: " + id);

        ZoneId currentZone = ZoneId.systemDefault();
        System.out.println("当期时区: " + currentZone);
    }

    @Test
    public void testDateComparator() {
        LocalDate date1 = LocalDate.parse("2020-07-01");
        LocalDate date2 = LocalDate.parse("2020-07-02");
        System.out.println(date1.compareTo(date2));
        System.out.println(date2.compareTo(date1));

        List<String> list = new ArrayList<>();
        list.add("2020-07-01");
        list.add("2020-07-02");
        list.stream().sorted((d1, d2) -> LocalDate.parse(d1).compareTo(LocalDate.parse(d2))).forEach(System.out::println);
        list.stream().sorted((d1, d2) -> LocalDate.parse(d2).compareTo(LocalDate.parse(d1))).forEach(System.out::println);
    }
}
